Re: [SQL] DEFAULT confusion - Mailing list pgsql-sql

From Herouth Maoz
Subject Re: [SQL] DEFAULT confusion
Date
Msg-id l03130300b3fbdacad26a@[147.233.159.109]
Whole thread Raw
In response to DEFAULT confusion  (Hroi Sigurdsson <hroi@ninja.dk>)
List pgsql-sql
At 04:39 +0200 on 08/09/1999, Hroi Sigurdsson wrote:


> INSERT INTO tabel VALUES (NEXTVAL('tabel_seq'), "sometext", 123);
>
> I can't just
> INSERT INTO tabel VALUES (NULL, "something", 123);

<RANT MODE="old teacher">
[tearing hair off in despair]

Children, how many times do I have to tell you that...
</RANT>

In an insert statement, you should always mention the names of the fields
to which you enter values. The syntax should be:

INSERT INTO table (field1, field2, field3)
VALUES (value1, value2, value3);

The SQL syntax allows you to drop the field names when you insert to *all*
of them. But this is NOT recommended, and should be used only in ad-hoc
insert statement done, say, in psql when you just have something to fix on
the fly.

In REAL applications, always mention the fields. This will prevent mixups
due to changes in the schema over time by giving you clear error messages
such as 'column not found' etc., instead of finding obscure data conversion
errors or none at all.

Now, what's the relevance of this lecture to your question?

If you don't mention the name of a field, and that field has a default
value, the default value will be used. It's that simple!

INSERT INTO stuff (name, number)
VALUES ("sometext", 123);

Will do the trick.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma




pgsql-sql by date:

Previous
From: "Moray McConnachie"
Date:
Subject: Re: [SQL] DEFAULT confusion
Next
From: Tom Lane
Date:
Subject: Re: [SQL] BUG with decimal type